using System.Web.Mvc;
using GoldenBicycleCompany.Core;
using SharpArch.Core.PersistenceSupport;
using SharpArch.Core.DomainModel;
using System.Collections.Generic;
using System;
using SharpArch.Web.NHibernate;
using NHibernate.Validator.Engine;
using System.Text;
using SharpArch.Web.CommonValidator;
using SharpArch.Core;

namespace GoldenBicycleCompany.Web.Controllers
{
    [HandleError]
    public class MaintenancesController : Controller
    {
        public MaintenancesController(IRepository<Maintenance> maintenanceRepository) {
            Check.Require(maintenanceRepository != null, "maintenanceRepository may not be null");

            this.maintenanceRepository = maintenanceRepository;
        }

        [Transaction]
        public ActionResult Index() {
            IList<Maintenance> maintenances = maintenanceRepository.GetAll();
            return View(maintenances);
        }

        [Transaction]
        public ActionResult Show(int id) {
            Maintenance maintenance = maintenanceRepository.Get(id);
            return View(maintenance);
        }

        public ActionResult Create() {
            MaintenanceFormViewModel viewModel = MaintenanceFormViewModel.CreateMaintenanceFormViewModel();
            return View(viewModel);
        }

        [ValidateAntiForgeryToken]
        [Transaction]
        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Create(Maintenance maintenance) {
            if (ViewData.ModelState.IsValid && maintenance.IsValid()) {
                maintenanceRepository.SaveOrUpdate(maintenance);

                TempData[ControllerEnums.GlobalViewDataProperty.PageMessage.ToString()] = 
					"The maintenance was successfully created.";
                return RedirectToAction("Index");
            }

            MaintenanceFormViewModel viewModel = MaintenanceFormViewModel.CreateMaintenanceFormViewModel();
            viewModel.Maintenance = maintenance;
            return View(viewModel);
        }

        [Transaction]
        public ActionResult Edit(int id) {
            MaintenanceFormViewModel viewModel = MaintenanceFormViewModel.CreateMaintenanceFormViewModel();
            viewModel.Maintenance = maintenanceRepository.Get(id);
            return View(viewModel);
        }

        [ValidateAntiForgeryToken]
        [Transaction]
        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Edit(Maintenance maintenance) {
            Maintenance maintenanceToUpdate = maintenanceRepository.Get(maintenance.Id);
            TransferFormValuesTo(maintenanceToUpdate, maintenance);

            if (ViewData.ModelState.IsValid && maintenance.IsValid()) {
                TempData[ControllerEnums.GlobalViewDataProperty.PageMessage.ToString()] = 
					"The maintenance was successfully updated.";
                return RedirectToAction("Index");
            }
            else {
                maintenanceRepository.DbContext.RollbackTransaction();

				MaintenanceFormViewModel viewModel = MaintenanceFormViewModel.CreateMaintenanceFormViewModel();
				viewModel.Maintenance = maintenance;
				return View(viewModel);
            }
        }

        private void TransferFormValuesTo(Maintenance maintenanceToUpdate, Maintenance maintenanceFromForm) {
			maintenanceToUpdate.Bicycle = maintenanceFromForm.Bicycle;
			maintenanceToUpdate.ProblemDescription = maintenanceFromForm.ProblemDescription;
			maintenanceToUpdate.ProblemResolution = maintenanceFromForm.ProblemResolution;
			maintenanceToUpdate.RepairStartTime = maintenanceFromForm.RepairStartTime;
			maintenanceToUpdate.RepairEndTime = maintenanceFromForm.RepairEndTime;
        }

        [ValidateAntiForgeryToken]
        [Transaction]
        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Delete(int id) {
            string resultMessage = "The maintenance was successfully deleted.";
            Maintenance maintenanceToDelete = maintenanceRepository.Get(id);

            if (maintenanceToDelete != null) {
                maintenanceRepository.Delete(maintenanceToDelete);

                try {
                    maintenanceRepository.DbContext.CommitChanges();
                }
                catch {
                    resultMessage = "A problem was encountered preventing the maintenance from being deleted. " +
						"Another item likely depends on this maintenance.";
                    maintenanceRepository.DbContext.RollbackTransaction();
                }
            }
            else {
                resultMessage = "The maintenance could not be found for deletion. It may already have been deleted.";
            }

            TempData[ControllerEnums.GlobalViewDataProperty.PageMessage.ToString()] = resultMessage;
            return RedirectToAction("Index");
        }

		/// <summary>
		/// Holds data to be passed to the Maintenance form for creates and edits
		/// </summary>
        public class MaintenanceFormViewModel
        {
            private MaintenanceFormViewModel() { }

			/// <summary>
			/// Creation method for creating the view model. Services may be passed to the creation 
			/// method to instantiate items such as lists for drop down boxes.
			/// </summary>
            public static MaintenanceFormViewModel CreateMaintenanceFormViewModel() {
                MaintenanceFormViewModel viewModel = new MaintenanceFormViewModel();
                
                return viewModel;
            }

            public Maintenance Maintenance { get; internal set; }
        }

        private readonly IRepository<Maintenance> maintenanceRepository;
    }
}
